package com.bangbang.controller;


import com.alibaba.fastjson.JSONObject;
import com.bangbang.service.ITbLabelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 标签表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-08-12
 */
@RestController
@RequestMapping("/bangbang/tbLabel")
@Api
@Configurable
@EnableScheduling
public class TbLabelController {
    @Autowired
    private ITbLabelService labelService;
    @Autowired
    private RedisTemplate redisTemplate;


    @GetMapping("insertLabel")
    @ApiOperation(value = "增加标签")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid",value = "用户id",required = true,paramType = "query"),
            @ApiImplicitParam(name = "labelid",value = "标签id",required = true,paramType = "query")
    })
    public boolean insertLabel(@RequestParam(required = true) Integer userid,@RequestParam(required = true) Integer labelid){
        //获取用户id
        String numstr = userid + "-" + "num";
        String timestr = userid + "-" + "time";
        if (redisTemplate.boundZSetOps(numstr).getKey().toString().equals(numstr)){
            Set set = redisTemplate.boundZSetOps(numstr).reverseRange(0, -1);
            boolean contains = set.contains(labelid);
            if (contains){
                Double aDouble = redisTemplate.boundZSetOps(numstr).incrementScore(labelid, 1);
                System.out.println(aDouble);
            }else {
                redisTemplate.boundZSetOps(userid + "-" + "num").add(labelid,1);
            }
        }else {
            redisTemplate.boundZSetOps(userid + "-" + "num").add(labelid,1);
        }
        //记录浏览时间

       if (redisTemplate.boundZSetOps(timestr).getKey().toString().equals(timestr)){
           Set set = redisTemplate.boundZSetOps(timestr).reverseRange(0, -1);
           boolean contains = set.contains(labelid);
           if (contains){
               redisTemplate.boundZSetOps(timestr).remove(labelid);
               String yyyyMMddHHmmssSSS = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"));
               long timeBiao = Long.parseLong(yyyyMMddHHmmssSSS);
               redisTemplate.boundZSetOps(timestr).add(labelid,timeBiao);
           }else {
               String yyyyMMddHHmmssSSS = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"));
               long timeBiao = Long.parseLong(yyyyMMddHHmmssSSS);
               redisTemplate.boundZSetOps(timestr).add(labelid,timeBiao);
           }
        }else {
            String yyyyMMddHHmmssSSS = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"));
            long timeBiao = Long.parseLong(yyyyMMddHHmmssSSS);
            redisTemplate.boundZSetOps(timestr).add(labelid,timeBiao);
        }

        //获取用户访问的商品标签
        //接收用户访问的商品id
        //查询该商品的标签
        //以用户id + “-”+ 浏览量标识（num业务的key）为key，
        // 以标签名为value，以1为分数（进行存放之间先判断是否有该标签，
        //Object key = redisTemplate.boundZSetOps(userid + "-" + "num").getKey();
        //Double score = redisTemplate.boundZSetOps(userid + "-" + "num").score(labelid);
       // System.out.println(score);
        //System.out.println(key);
        //排序
        /*Set<ZSetOperations.TypedTuple<String>> reportCodes = redisTemplate
                .boundZSetOps("myzset")
                .reverseRangeWithScores(1,3);*/
        //删除指定分数范围内的
        return true;

    }

    public static void main(String[] args) {
        /*String yyyyMMddHHmmssSSS = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"));
        System.out.println(yyyyMMddHHmmssSSS);*/
        /*Long str1 =Long.parseLong("20210813151949375");
        Long str2 =Long.parseLong("20210813152005416");
                //"20210813151949375".toString();

        if (str1 <str2 ){
            System.out.println("成功");
        }*/
       // boolean ceshi = ceshi();
        System.out.println();

    }
    @GetMapping("selectLabel")
    @ApiOperation(value = "查询定向推荐的标签")
    public List<Object> selectLabel(){
        List<Object> list = new ArrayList<>();
        Set indexf = redisTemplate.boundZSetOps("index").reverseRange(0, -1);
        for (Object o : indexf) {
            Double index1 = redisTemplate.boundZSetOps("index").score(o);
            String s = JSONObject.toJSONString(o);
            System.out.println(s);
            list.add(o);
        }
        return list;
    }

}

